package mystack;

import java.util.Arrays;

/**
 * @author 兴趣使然黄小黄
 * @version 1.0
 * 使用数组模拟顺序栈
 */
@SuppressWarnings({"all"})
public class MyStack {

    private int[] elements;
    private int useSize;
    private final int DEFAULT_CAPACITY = 10;

    public MyStack(){
        elements = new int[DEFAULT_CAPACITY];
        useSize = 0;
    }

    // 入栈
    public void push(int data){
        // 判断是否满 没有满才能入栈
        if (isFull()){
            // 扩容
            elements = Arrays.copyOf(elements, elements.length + DEFAULT_CAPACITY);
        }
        // 入栈操作
        elements[useSize++] = data;
    }

    // 出栈
    public int pop(){
        // 栈空不能出栈
        if (isEmpty()){
            throw new RuntimeException("pop()时栈为空");
        }
        // 出栈
        return elements[--useSize];
    }

    // 查看栈顶元素
    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("peek()时栈为空");
        }
        return elements[useSize-1];
    }

    // 获取栈的大小
    public int size(){
        return useSize;
    }

    // 判断栈是否为空
    public boolean isEmpty(){
        return useSize == 0;
    }

    // 判断是否满
    public boolean isFull(){
        return useSize == elements.length;
    }

}
